package com.weplay.gameplane.utils;

import org.jetbrains.annotations.NotNull;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 原文链接：https://blog.csdn.net/mjf5929/article/details/125808347
 */
public class HolidayUtil {


    /**
     * 判断当前是否为节假日： 0 上班 1周末 2节假日
     *
     * @param httpArg :参数
     * @return 返回结果
     */

    public static void request(String httpArg, NetWorkCallback netWorkCallback) {
        String httpUrl = "http://tool.bitefu.net/jiari/" + "?d=" + httpArg;

        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(5, TimeUnit.SECONDS)//链接超时为2秒，单位为秒
                .writeTimeout(5, TimeUnit.SECONDS)//写入超时
                .readTimeout(5, TimeUnit.SECONDS)//读取超时
                .build();

        Request request = new Request.Builder()
                .url(httpUrl)//URL地址
                .get()
                .build();//构建

        //异步
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                netWorkCallback.onCallback("0");
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String result = response.body().string();
                netWorkCallback.onCallback(result);

            }
        });

    }

    public interface NetWorkCallback{
        void onCallback(String result );
    }

}
